import { _decorator, Component, log, Node,Animation, tween,v3, CCInteger } from 'cc';
import { GameManager } from './GameManager';
import { AudioManager } from './AudioManager';
const { ccclass, property } = _decorator;

@ccclass('dogC1')
export class dogC1 extends Component {
     //状态100是动画走过来，101 是表情，102是跳起来，103 是隐藏起来 200是赢了一个鸭子，300是赢了两个鸭子，400是嘲笑你
    zhuantai:number = 103;

    //初始坐标
    startNode: number[] = [-543,-300];

    @property(CCInteger)
    speed:number = 150;

    //保存动画组件
    private _animation: Animation | null = null;
    onLoad() {
        // 获取Animation组件
        this._animation = this.node.getComponent(Animation);
    }

    start() {
        // this.initDog();
    }



    initDog() {
        log("initDog",this.startNode[0],this.startNode[1])

//播放声音
        AudioManager.instance.playSFX('sound/bg', false);

        this.node.setPosition(this.startNode[0],this.startNode[1]); 
        this.node.setScale(1,1,1);
        this.node.active = true;
       this.zhuantai = 100;
         if (this._animation && this._animation.clips.some(clip => clip.name === 'dogRun')) {
            this._animation.play('dogRun');
        }
    }

   private dogRun2() {
        log("dogRun2",this._animation.clips)
        if (this._animation && this._animation.clips.some(clip => clip.name === 'dogRun2')) {
            this._animation.play('dogRun2');
           
             this.scheduleOnce(function() {
                this._animation.play('dogRun3');
                this.zhuantai = 102;
            }, 1);
        }
    }
   private dogRun3() {
        tween(this.node)
            .to(0.5, { scale: v3(0, 0, 0)}) 
            .by(0.3, { position: v3(0, -200, 0) }) // 位置是相对当前向下移动20像素
            .call(() => {
                this.node.active = false; // 隐藏节点
                // this.node.destroy(); // 销毁节点（请谨慎使用）
                log('精灵已缩小并消失。');
                GameManager.instance.paying2();
            })
            .start();
    }
    update(deltaTime: number) {
        if(this.zhuantai == 100){
            this.node.x += this.speed * deltaTime;
            if(this.node.x > 0){
              this.dogRun2()
              this.zhuantai = 101;
            }
        }
         if(this.zhuantai == 102){
            this.node.y += this.speed * deltaTime;
            if(this.node.y > -177){
                this.dogRun3()
               this.zhuantai = 103;
            }
        }
    }
}


